package com.sltx.service.impl;

import com.sltx.dao.aaa.QypjMapper;
import com.sltx.service.QypjService;
import com.sltx.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author Administrator
 * @Date 2023/4/12 9:19
 */
@Service
@Transactional
public class QypjServiceImpl implements QypjService {

    @Autowired
    private QypjMapper qypjMapper;

    //获取优质企业评价
    @Override
    public Map<String, Object> getYouzhiqy(String entName) {
        Map<String, Object> map = new HashMap<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        //1.通过传来的企业名称查询优质企业分项指标
        List<Map<String, Object>> Index = qypjMapper.getYouzhi(entName);
        if(Index.size() == 0){
           return map;
        }
        for (Map<String, Object> map1 : Index) {
            String SCORE = map1.get("SCORE").toString();
            map1.put("SCORE",SCORE);
        }
        //根据企业名称获取优质企业评价
       List<Map<String,Object>> Evaluate =  qypjMapper.getYouzhipingjia(entName);
        if (Evaluate.size() == 0){
            return map;
        }
        for (Map<String, Object> map2 : Evaluate) {
            String SCORE2 = map2.get("SCORE").toString();
            map2.put("SCORE",SCORE2);
        }
        //根据企业名称和季度获取最新季度榜单排名
       Integer Paimingxin = qypjMapper.getPaimingxin(entName);
       //根据企业名称和季度获取上一个季度榜单排名
       Integer Paimingqian = qypjMapper.getPaimingqian(entName);
        //如果最新季度比上一季度大，那就存入0（下降）1（上升）2（不变）
       if (Paimingqian == null) {
           hashMap.put("bubian", 2);
           Evaluate.add(hashMap);
           //如果最新季度比上一季度大，那就存入0（下降）1（上升）2（不变）
       }else if (Paimingxin > Paimingqian) {
           hashMap.put("bubian", 0);
           Evaluate.add(hashMap);
       } else if (Paimingxin < Paimingqian) {
           hashMap.put("bubian", 1);
           Evaluate.add(hashMap);
       } else if (Paimingxin == Paimingqian) {
           hashMap.put("bubian", 2);
           Evaluate.add(hashMap);
       }
        //优质企业评价
        map.put("Evaluate",Evaluate);
       //优质企业分项指标
       map.put("Index",Index);
       return map;
    }

    //获取潜力企业评价
    @Override
    public Map<String, Object> getQianli(String entName) {
        Map<String, Object> map = new HashMap<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        //1.通过传来的企业名称查询潜力企业分项指标
        List<Map<String, Object>> Index = qypjMapper.getYouzhiqianli(entName);
        if(Index.size() == 0){
            return map;
        }
        for (Map<String, Object> map1 : Index) {
            String SCORE2 = map1.get("SCORE").toString();
            map1.put("SCORE",SCORE2);
        }
        //根据企业名称获取潜力企业评价
        List<Map<String,Object>> Evaluate =  qypjMapper.getYouzhipingjiaqianli(entName);
        if(Evaluate.size() == 0){
            return map;
        }
        for (Map<String, Object> map2 : Evaluate) {
            String SCORE2 = map2.get("SCORE").toString();
            map2.put("SCORE",SCORE2);
        }
        //根据企业名称和季度获取最新季度榜单排名
        Integer Paimingxin = qypjMapper.getPaimingxinqianli(entName);
        //根据企业名称和季度获取上一个季度榜单排名
        Integer Paimingqian = qypjMapper.getPaimingqianqianli(entName);
        if (Paimingqian == null) {
            hashMap.put("bubian", 2);
            Evaluate.add(hashMap);
            //如果最新季度比上一季度大，那就存入0（下降）1（上升）2（不变）
        }else if (Paimingxin > Paimingqian){
            hashMap.put("bubian",0);
            Evaluate.add(hashMap);
        }else if (Paimingxin < Paimingqian){
            hashMap.put("bubian",1);
            Evaluate.add(hashMap);
        }else if (Paimingxin == Paimingqian){
            hashMap.put("bubian",2);
            Evaluate.add(hashMap);
        }
        //潜力企业评价
        map.put("Evaluate",Evaluate);
        //潜力企业分项指标
        map.put("Index",Index);
        return map;
    }

    //预警企业风险
    @Override
    public Map<String, Object> getYujing(PageData pd) {
            Map<String, Object> map = null;
            map = new HashMap<>();
            String entName = (String) pd.get("entName");
            //获取此企业最新月份和预警项
            Map<String, Object> Listdata = qypjMapper.getFengxian(entName);
            String listdate = (String) Listdata.get("LISTDATE"); //获取此预警企业最新月份
            //获取当前日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            Date date1 = new Date();
            String format = sdf.format(date1); //获取当前时间 2023年05月
            //将字符串转为date类
            Calendar rightNow = Calendar.getInstance(); //获取一个对象对日期换算
            rightNow.setTime(date1); //把转换为日期类型的日期传入进去
            rightNow.add(Calendar.MONTH,-1); //将此日期的月份-1
            Date time = rightNow.getTime(); //用此对象获取它的时间
            String date = sdf.format(time); //转换为字符串 //2023年04月
            if (listdate.equals(format) || listdate.equals(date)){
              map.put("date","1"); //1代表此企业可以上升下降 2代表不可以
            }else {
                map.put("date","2"); //1代表此企业可以上升下降 2代表不可以
            }
            //获取企业分项指标
            List<Map<String,Object>> Index =  qypjMapper.getZhibiao1(entName);
            map.put("ListData",Listdata); //预警风险
            map.put("Index",Index); //分项指标
            return map;

    }

    //企业分项指标详细数据
    @Override
    public Map<String, Object> getShuju(PageData pd) {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        //通过月份，预警项，用户名，企业名
        //1.先查询案号，执行法院，执行标的（元），立案时间
        String date1 = (String) pd.get("date"); //时间
        try {
        Date parse = sdf.parse(date1); //将字符串转为date类型
        Calendar rightNow = Calendar.getInstance(); //获取一个对象对日期换算
        rightNow.setTime(parse); //把转换为日期类型的日期传入进去
        rightNow.add(Calendar.MONTH,-1); //将此日期的月份-1
        Date time = rightNow.getTime(); //用此对象获取它的时间
        String date = sdf.format(time); //转换为字符串
        String entName = (String) pd.get("entName"); //企业名称
        String yuJing = (String) pd.get("yuJing"); //预警项
        //String userId = (String) pd.get("userId");
        //查询法院执行
        List<Map<String,Object>> Execute = qypjMapper.getZhibiao2(date,entName,yuJing);
        map.put("Execute",Execute);
        //查询变更记录
        List<Map<String,Object>> Record = qypjMapper.getZhibiao3(date,entName,yuJing);
        map.put("Record",Record);
        //经营异常
        List<Map<String,Object>> Abnormal = qypjMapper.getZhibiao4(date,entName,yuJing);
        map.put("Abnormal",Abnormal);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return map;
    }

    //查询此企业为什么企业
    @Override
    public Map<String, Object> getQiye(PageData pd) {
        HashMap<String, Object> map = new HashMap<>();
        //查询是否为优质企业
       String youzhi =  qypjMapper.getYouzhiqiye(pd);
        if (youzhi != null){
            map.put("QYBZ",youzhi);
            map.put("QYPY","yzqy");

            return map;
        }
       //查询是否为潜力企业
       String qianli = qypjMapper.getQianliqiye(pd);
        if (qianli != null){
           map.put("QYBZ",qianli);
           map.put("QYPY","qlqy");
           return map;
        }
        //查询是否为预警企业
        String yujing = qypjMapper.getYujingqiye(pd);
        if (yujing != null){
            map.put("QYBZ","预警企业");
            map.put("QYPY","yjqy");
            return map;
        }
        //如果都不是返回0
        map.put("QYBZ","0");
        return map;
    }


}
